using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InvoiceProject.Enums;
using InvoiceProject.Models;

namespace InvoiceProject.Controllers
{   
    public class BusinessesController : BaseController
    {
		private readonly IBusinessRepository businessRepository;

        public BusinessesController(IBusinessRepository businessRepository)
        {
			this.businessRepository = businessRepository;
        }

        //
        // GET: /Businesses/

        public ViewResult Index()
        {
            return View(businessRepository.All);
        }

        //
        // GET: /Businesses/Details/5

        public ViewResult Details(long id)
        {
            return View(businessRepository.Find(id));
        }

        //
        // GET: /Businesses/Create

        public ActionResult Create()
        {

            //var viewModel = new BusinessViewModelCreateUpdate();
            //viewModel.Business = new Business();
            //viewModel.Business.AccountType = AccountType.Month;
            Business model = new Business();
            model.LookupTaxCountry = new LookupTaxCountry{Country = Country.AUS,DateModified = DateTime.Now,UserModified = "ME",TaxName = "GST",TaxRate = .1M};// {  Country = Co, TaxRate = .1M , DateModified= DateTime.Now,TaxName = "dsfds",UserModified = "sdfds"};
            setForSave(model);
            setForSave(model.Address);

            return View(model);
        } 

        //
        // POST: /Businesses/Create

        public void setForSave(BaseModel model)
            {
                //model.DateCreated = DateTime.Now;
                model.DateModified = DateTime.Now;
               // model.UserCreated = "FF";
                model.UserModified = "FF";
            }

        [HttpPost]
        public ActionResult Create(Business model)
        {

model.LookupTaxCountry.GetTaxDetailsByCountry();

            ModelErrorCollection error = new ModelErrorCollection();
            foreach (var states in ModelState)
            {
                foreach (var err in states.Value.Errors)
                {
                    error.Add(err);   
                }
            }

            if (ModelState.IsValid) {
                 
           
                //UpdateEntityForSave(model.Business, "IF0045");
                //UpdateEntityForSave(model.Business.Address, "IF0045");

                businessRepository.InsertOrUpdate(model);
                businessRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View(model);
			}
        }
        
        //
        // GET: /Businesses/Edit/5
 
        public ActionResult Edit(long id)
        {
             return View(businessRepository.Find(id));
        }

        //
        // POST: /Businesses/Edit/5

        [HttpPost]
        public ActionResult Edit(Business business)
        {
            if (ModelState.IsValid) {
                businessRepository.InsertOrUpdate(business);
                businessRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }

        //
        // GET: /Businesses/Delete/5
 
        public ActionResult Delete(long id)
        {
            return View(businessRepository.Find(id));
        }

        //
        // POST: /Businesses/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            businessRepository.Delete(id);
            businessRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

